/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.facebook.presto.spi.adaptive.metadata.presto;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
 * @author luo
 * PrestoTable is a pojo that package catalog, schema, table and some.
 */
public class PrestoTable
        implements Serializable
{
    private static final long serialVersionUID = 4637266315650381346L;

    private String catalog;
    private String schema;
    private String table;
    private List<String> mongoIndex;
    private String hbaseRowkey;

    public PrestoTable(String catalog, String schema, String table)
    {
        this.catalog = catalog;
        this.schema = schema;
        this.table = table;
    }

    private PrestoTable(Builder builder)
    {
        this.catalog = builder.catalog;
        this.schema = builder.schema;
        this.table = builder.table;
        this.mongoIndex = builder.mongoIndex;
        this.hbaseRowkey = builder.hbaseRowkey;
    }

    public String getCatalog()
    {
        return catalog;
    }

    public String getSchema()
    {
        return schema;
    }

    public String getTable()
    {
        return table;
    }

    public List<String> getMongoIndex()
    {
        return mongoIndex;
    }

    public String getHbaseRowkey()
    {
        return hbaseRowkey;
    }

    @Override
    public String toString()
    {
        return "PrestoTable{" +
                "catalog='" + catalog + '\'' +
                ", schema='" + schema + '\'' +
                ", table='" + table + '\'' +
                ", mongoIndex=" + mongoIndex +
                ", hbaseRowkey='" + hbaseRowkey + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o)
    {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        PrestoTable that = (PrestoTable) o;
        return Objects.equals(catalog, that.catalog) &&
                Objects.equals(schema, that.schema) &&
                Objects.equals(table, that.table);
    }

    public boolean equals(String catalog, String schema, String table)
    {
        return this.catalog.equals(catalog) &&
                this.table.equals(table);
    }

    public static class Builder
    {
        private String catalog;
        private String schema;
        private String table;
        private List<String> mongoIndex;
        private String hbaseRowkey;

        public Builder(String catalog, String schema, String table)
        {
            this.catalog = catalog;
            this.schema = schema;
            this.table = table;
        }

        public Builder setMongoIndex(List<String> mongoIndex)
        {
            this.mongoIndex = mongoIndex;
            return this;
        }

        public Builder setHBaseRowkey(String hbaseRowkey)
        {
            if (hbaseRowkey.startsWith("'") && hbaseRowkey.endsWith("'")) {
                this.hbaseRowkey = hbaseRowkey.substring(1, hbaseRowkey.length() - 1);
                return this;
            }
            this.hbaseRowkey = hbaseRowkey;
            return this;
        }

        public PrestoTable build()
        {
            return new PrestoTable(this);
        }
    }
}
